from django.contrib import admin
from django.apps import apps
from django import forms
from django.contrib import admin
class AuthorForm(forms.Form):
    account = forms.CharField()
    name = forms.CharField()
    role = forms.CharField()

class AuthorListForm(forms.ModelForm):
    authors = forms.CharField(widget=forms.Textarea, initial='[]')

class ReportModelAdmin(admin.ModelAdmin):
    form = AuthorListForm
    change_form_template = 'admin/reportmodel_change_form.html'  # 指定自定义模板

    def get_form(self, request, obj=None, **kwargs):
        form = super().get_form(request, obj, **kwargs)
        if obj and hasattr(form, 'base_fields') and 'authors' in form.base_fields:
            form.base_fields['authors'].initial = getattr(obj, 'authors', None)
        return form

    def save_model(self, request, obj, form, change):
        authors_data = form.cleaned_data['authors']
        obj.authors = authors_data
        super().save_model(request, obj, form, change)

# 获取当前应用的所有模型
model_list = apps.get_app_config('ReportManager').get_models()

# 为每个模型注册管理界面
for model in model_list:
    admin.site.register(model, ReportModelAdmin)
